﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using vJine.Core;
using vJine.Core.Base;
using vJine.Core.ORM;
using vJineCore_QuickStart.BML;

namespace vJineCore_QuickStart {
    public class QuickStart_ORM : QuickStart {
        public override void Start() {

            this.IDUQ("SQLite");
            //this.IDUQ("ACCESS");
            //this.IDUQ("MySQL");
            //this.IDUQ("PGSQL");
            //this.IDUQ("MSSQL");
            //this.IDUQ("Oracle");

            this.CreateDataManager();
        }

        /// <summary>
        /// 增删改查示例
        /// </summary>
        /// <param name="db">数据库连接名称</param>
        void IDUQ(string db) {
            DataManager dm = new DataManager(db);
            dm.OnTrace += (int ID, string Action, string Table, string Cmd, DbParameterCollection Params) => {
                System.Console.WriteLine("ID:{0},ACTION:{1},TABLE:{2},CMD:{3}", ID, Action, Table, Cmd);

                return null;
            };

            //创建表
            dm.Create<BizEntity>(true);
            string sql_create = dm.ToString();


            //新增数据
            dm.I<BizEntity>(
                new BizEntity() { Name = "Name_1", Number = 1, ENUM = Enum_Type.E01 });
            dm.I<BizEntity>(
                new BizEntity() { Name = "Name_2", Number = 2, ENUM = Enum_Type.E02 });
            string sql_insert = dm.ToString();


            //查询数据
            CollectionBase<BizEntity> container = new CollectionBase<BizEntity>();
            dm.Q<BizEntity>(container,
                BizEntity._.Name == "Name_1" | BizEntity._.Name == "Name_2");
            string sql_select = dm.ToString();


            //修改数据
            dm.U<BizEntity>(
                BizEntity._.Name.EQ("Name_11") /*设置更新值*/,
                BizEntity._.Name == "Name_1"   /*设置查询条件*/);
            string sql_update = dm.ToString();


            //删除数据
            dm.D<BizEntity>(BizEntity._.Name == "Name_11" | BizEntity._.Name == "Name_2");
            string sql_delete = dm.ToString();


            //删除表
            dm.Drop<BizEntity>();
            string sql_drop = dm.ToString();
        }

        void CreateDataManager() {

            DataManager dm = null;

            {//内置定义：默认Default为SQLite
                dm = new DataManager();
            }

            {//内置定义：默认连接
                dm = new DataManager("SQLite");
                dm = new DataManager("ACCESS");
                dm = new DataManager("MySQL");
                dm = new DataManager("PGSQL");
                dm = new DataManager("MSSQL");
                dm = new DataManager("Oracle");
            }

            {//自定义：
                dm = new DataManager("SQLite_MyCustom");
                dm = new DataManager("ACCESS_MyCustom");
                dm = new DataManager("MySQL_MyCustom");
                dm = new DataManager("PGSQL_MyCustom");
                dm = new DataManager("MSSQL_MyCustom");
                dm = new DataManager("Oracle_MyCustom");
            }

            {
            }
        }
    }
}
